@inherits VocaDb.Web.Code.HelperPage
@using System.Web.Mvc.Html;
@using Autofac.Integration.Mvc
@using Microsoft.Web.Helpers
@using Resources
@using VocaDb.Model.DataContracts.Users
@using VocaDb.Model.Domain.Images
@using VocaDb.Model.Domain.Users
@using VocaDb.Model.Helpers
@using VocaDb.Web.Helpers

@functions {

	static string GetImageSizeProperty(ImageSize size) {
		switch (size) {
			case ImageSize.Thumb:
				return "urlThumb";
			case ImageSize.SmallThumb:
				return "urlSmallThumb";
			case ImageSize.TinyThumb:
				return "urlTinyThumb";
		}
		return "urlThumb";
	}

}

<!-- User icon inside anchor (no name) -->
@helper IconAndLinkKnockout(string userBinding, string cssClass) {
	<a data-bind="attr: { href: vdb.utils.EntryUrlMapper.details_user_byName(@(userBinding + ".name")) }" class="pull-left">
		@ProfileIconKnockout(VocaDb.Model.Domain.Images.ImageSize.Thumb, binding: userBinding, size: 70)
	</a>
}

@helper IconAndNameKnockout(string iconBinding, string nameBinding, int size = 20) {
	@ProfileIconKnockout(iconBinding, size)
	<span data-bind="text: @nameBinding"></span>
}

@helper IconAndNameLinkKnockout(int iconSize = 20, string cssClass = "") {
	@* Has to be one line because of Razor *@
	<a href="#" class="@cssClass" data-bind="attr: { href: vdb.utils.EntryUrlMapper.details_user_byName(name) }">@ProfileIconKnockout("$data.mainPicture != null ? $data.mainPicture.urlThumb : null", iconSize)<span data-bind="text: name"></span></a>
}

@helper IconNameAndTypeLinkKnockout(string groupResources, int iconSize = 20) {
	<a href="#" data-bind="attr: { href: '/Profile/' + name, title: @(groupResources + "[groupId] + '\\nJoined: ' + memberSince") }">
		@ProfileIconKnockout("$data.mainPicture ? mainPicture.urlThumb : null", iconSize)
		<span data-bind="text: name"></span>
	</a>
}

<!-- User name inside anchor -->
@helper NameLinkKnockout(string userBinding) {
	<a href="#" data-bind="attr: { href: vdb.utils.EntryUrlMapper.details_user_byName(@(userBinding + ".name")) }"><span data-bind="text: @(userBinding + ".name")"></span></a>
}

@helper ProfileIconKnockout(string binding, int size = 80) {
	<div data-bind="visible: @binding" style="width: @(size)px; height: @(size)px; display: inline-block">
		<img data-bind="attr: { src: @binding }" alt="User avatar" src="@Url.Content("~/Content/unknown.png")" />
	</div>
}

@helper SongVoteRatingsRadioKnockout(string binding) {
	<button class="btn" data-bind="click: function() { @(binding)('Nothing'); }, css: { active: @(binding)() == 'Nothing' }">@ViewRes.User.RatedSongsStrings.RatingAnything</button>
	<button class="btn" data-bind="click: function() { @(binding)('Like'); }, css: { active: @(binding)() == 'Like' }">
		<i class="icon-star"></i>
		@SongVoteRatingNames.Like
	</button>
	<button class="btn" data-bind="click: function() { @(binding)('Favorite'); }, css: { active: @(binding)() == 'Favorite' }">
		<i class="icon-heart"></i>
		@SongVoteRatingNames.Favorite
	</button>
}


@helper ProfileIcon(string url, int size = ImageHelper.UserThumbSize) {
	if (!string.IsNullOrEmpty(url)) {
		<div style="width: @(size)px; height: @(size)px; display: inline-block">
			<img src="@url" alt="Icon" />
		</div>
	}
}

@helper IconAndName(IUserWithEmail user, int size = 20) {
	@ProfileIcon(user, size)
	<span>@user.Name</span>
}

@helper IconAndName(IUserWithEmail user, string name, int size = 20) {
	if (user != null) {
		@IconAndName(user, size)
	} else {
		@name
	}
}

@helper ProfileIcon(IUserWithEmail user, int size = 80) {
	if (user != null && !string.IsNullOrEmpty(user.Email)) {
		<div style="width: @(size)px; height: @(size)px; display: inline-block">
			@Gravatar.GetHtml(user.Email, size, scheme: RequestUrlScheme)
		</div>
	}
}

@helper ProfileIconKnockout(ImageSize imageSize = ImageSize.TinyThumb, string binding = "$data", int size = 0) {
	@ProfileIconKnockout(binding + ".mainPicture != null ? " + binding + ".mainPicture." + GetImageSizeProperty(imageSize) + " : null", size > 0 ? size : ImageHelper.GetUserImageSizePx(imageSize))
}

@helper UserIconLink(IUserWithEmail user, int size = ImageHelper.UserTinyThumbSize) {
	<a href="@Url.UserDetails(user)">@ProfileIcon(user, size)<span>@(user != null ? user.Name : "")</span></a>
}

@helper UserIconLink(UserContract user, int size = ImageHelper.UserTinyThumbSize, bool userInfo = false, bool tooltip = false) {
	<a href="@Url.UserDetails(user)" @Html.ConditionalAttribute(tooltip && user != null, "data-bind", "userToolTip: " + (user != null ? user.Id : 0)) 
	   title='@(userInfo && user != null ? (Translate.UserGroups[user.GroupId] + "\nJoined: " + user.CreateDate.ToShortDateString()) : "")'>@ProfileIcon(user, size)<span>@(user != null ? user.Name : "")</span></a>
}

@helper UserIconLink(UserForApiContract user, int size = ImageHelper.UserTinyThumbSize, bool userInfo = false, bool tooltip = false) {
	<a href="@Url.UserDetails(user)" @Html.ConditionalAttribute(tooltip && user != null, "data-bind", "userToolTip: " + (user != null ? user.Id : 0)) 
	   title='@(userInfo && user != null ? (Translate.UserGroups[user.GroupId] + "\nJoined: " + user.MemberSince.ToShortDateString()) : "")'>
		@ProfileIcon(user != null && user.MainPicture != null ? user.MainPicture.GetSmallestThumb(ImageSize.TinyThumb) : null, size)
		<span>@(user != null ? user.Name : "")</span>
	</a>
}

@helper UserIconLink(UserForApiContract user, string name, int size = 20) {
	if (user != null) {
		@UserIconLink(user, size)
	} else {
		@name
	}
}

@helper UserIconLink(IUserWithEmail user, string name, int size = 20) {
if (user != null) {
		@UserIconLink(user, size)
	} else {
		@name
	}
}

@helper UserLink(IUser user) {
	@Html.ActionLink(user.Name, "Profile", "User", new { id = user.Name }, null)
}

@helper UserLink(IUser user, string name) {
	if (user != null) {
		@UserLink(user)
	} else {
		@name
	}
}